<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm(){
		var avgSingle = document.getElementById("avgSingle");
		var stageNames = document.getElementsByName("stageName");
		var stageDescs = document.getElementsByName("stageDesc");
		var successRates = document.getElementsByName("successRate");
		
		if(avgSingle.value == "" || avgSingle.value == "0"){
			alert("<fmt:message key='stage.lable.avg'/><fmt:message key='common.message.empty'/>");
			return false;
		}
		
		for(var i = 0; i < stageNames.length; i++){
			if(stageNames[i].value != "" 
				|| stageDescs[i].value != "" 
				|| successRates[i].value != ""){
				
				if(stageNames[i].value == ""){
					alert("<fmt:message key='stage.lable.saleStage'/><fmt:message key='common.message.empty'/>");
					return false;
				}
				if(stageDescs[i].value == ""){
					alert("<fmt:message key='stage.lable.stageDesc'/><fmt:message key='common.message.empty'/>");
					return false;
				}
				if(successRates[i].value == ""){
					alert("<fmt:message key='stage.lable.successRate'/><fmt:message key='common.message.empty'/>");
					return false;
				}
				if(isRepeat(i,stageNames[i].value)){
					alert("<fmt:message key='stage.message.repeat'/>");
					return false;
				}
			}
		}
		return true;
	}
	
	function isRepeat(num,name){
		var stageNames = document.getElementsByName("stageName");
		var stageDescs = document.getElementsByName("stageDesc");
		var successRates = document.getElementsByName("successRate");
		
		for(var i = 0; i < stageNames.length; i++){
			if(i != num && (stageNames[i].value != "" || stageDescs[i].value != "" || successRates[i].value != "")){
				if(stageNames[i].value == name){
					return true;
				}
			}
		}
		return false;
	}

	function back(){
		window.location.href = "./saleStageSet.do";
		window.location.target = "main";
	}
	
	function addRow(tableId){//增加tab的一行	
		try{				
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
			var insertTdHtml = prepareInsertHtml(newTrNum);//获得新增行各个TD的HTML代码
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	var insertHtml = new Array();//用于插入新记录的HTML
	function prepareInsertHtml(num){
		//insertHtml[0] = "<input type='hidden' name='ids' value='"+ num+"'/>"+num;	
		insertHtml[0] = "<input id='stageName' name='stageName' class='input2' type='text' size='15'/>";	
		insertHtml[1] = "<input id='stageDesc' name='stageDesc' class='input2' type='text' size='70'/>";	
		insertHtml[2] = "<input id='successRate' name='successRate' class='input2' type='text' size='5' onkeyup='onlyNumFloat(this)' />";	
		insertHtml[2] += "<input id='usefulTime' name='usefulTime' type='hidden' value='0' />";	
		return insertHtml;
	}
	
	function save(){
		if(checkForm()){
			document.updateSaleStageForm.action = "./saleStageSet.do?action=updateSaleStage";
			document.updateSaleStageForm.target = "main";
			document.updateSaleStageForm.submit();
		}
	}
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
		
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}
	}
-->
</script>
</head>
<body onload="initPage()">
<form name="updateSaleStageForm" action="" method="post">
	<table class="table_frame" cellpadding="0" cellspacing="0">
		<tr>
			<td>
				<table class="table_title" cellpadding="0" cellspacing="0">
					<tr>
						<td class="title_lab" colspan="3">
							&nbsp;&nbsp;&nbsp;&nbsp;<fmt:message key='stage.title.update'/>
						</td>
					</tr>
					<tr><td height="15"></td></tr>
					<tr>
						<td valign="bottom">&nbsp;
							<a href="javascript:save();" title="<fmt:message key='common.button.save'/>" hidefocus="true"><fmt:message key='common.button.save'/></a>&nbsp;&nbsp;
							<a href="javascript:addRow('table1');" title="<fmt:message key='common.button.append'/>" hidefocus="true"><fmt:message key='common.button.append'/></a>
						</td>
						<td valign="bottom" align="right">
							<a href="javascript:back();" title="返回" hidefocus="true">返回</a>&nbsp;&nbsp;&nbsp;&nbsp;
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td height="5"></td>
		</tr>
		<tr>
			<td>
				<table class="table_style" height="30" cellpadding="0" cellspacing="0">
					<tr>
						<td class="title3" width="12%"><fmt:message key='stage.lable.avg'/><fmt:message key='common.sign'/></td>
						<td class="title3">
							<input id="avgSingle" name="avgSingle" class="input1" type="text" size="8" value="${avgSingle.MValue}" onkeyup="onlyNumFloat(this)"/>
							<fmt:message key='common.lable.yuan'/>
						</td>
					</tr>
				</table>
				
				<table class="table_style">
					<tr><td class="title">信息</td></tr>
				</table>
				<table id="table1" class="table_style">
					<tr>
						<td class="title4"><fmt:message key='stage.lable.saleStage'/></td>
						<td class="title4" width="60%"><fmt:message key='stage.lable.stageDesc'/></td>
						<td class="title4" width="10%"><fmt:message key='stage.lable.successRate'/>(%)</td>
					</tr>
					<c:forEach var="saleStage" items="${saleStages}">
					<tr>
						<td><input id="stageName" name="stageName" class="input2" type="text" size="15" value="${saleStage.stageName}"/></td>
						<td><input id="stageDesc" name="stageDesc" class="input2" type="text" size="70" value="${saleStage.stageDesc}"/></td>
						<td>
							<input id="successRate" name="successRate" class="input2" type="text" size="5" value="${saleStage.successRate}" onkeyup="onlyNumFloat(this)"/>
							<input id="usefulTime" name="usefulTime" type="hidden" value="${saleStage.usefulTime}"/>
						</td>
					</tr>
					</c:forEach>
				</table>
			</td>
		</tr>
	</table>
</form>
</body>
</html>